# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

#
#
# @param root TreeNode类
# @return int整型
#
'''
核心就是遍历二叉树，然后一个参数来记录并累加
'''
class Solution:
    def sumNumbers(self , root):
        # write code here
        res = self.helper(root,0)
        return res
    def helper(self,root,prefix):
        if root == None:
            return 0
        prefix = prefix * 10 + root.val
        if root.left == None and root.right == None:
            return prefix
        return self.helper(root.left, prefix) + self.helper(root.right, prefix)